
#import "GetUserTorrentListRequest.h"

@implementation GetUserTorrentListRequest

//
// user id
//
@synthesize userId															= _userId;

//
// type
//
@synthesize type															= _type;

//
// torrent list
//
@synthesize torrentList														= _torrentList;


//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// init xml parser
	//
	self.xmlParser															= [[[XmlParser alloc] init] autorelease];
	self.torrentList														= [[[NSMutableArray alloc] initWithCapacity:50] autorelease];
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentList														= nil;
	self.type																= nil;
	
	[super dealloc];
}

//
// setup request
//
- (void)setupRequest
{
	//
	// setup rel path
	//
	self.relPath															= [NSString stringWithFormat:@"getusertorrentlistajax.php?userid=%d&type=%@", self.userId, self.type];
	
	//
	// call super
	//
	[super setupRequest];
}

//
// finished
//
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	if(self.response.statusCode == 200)
	{
		NSMutableData* fullData												= [NSMutableData dataWithData:self.dataBuffer];
		static char const* header											= "<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> </head><body>";
		static char const* footer											= "</body></html>";
		[fullData replaceBytesInRange:NSMakeRange(0, 0) withBytes:header length:strlen(header)];
		[fullData appendBytes:footer length:strlen(footer)];
		
		[self.xmlParser parseDocumentData:fullData htmlMode:YES];
		NSArray* torrentRows												= [self.xmlParser nodesFromPath:@"//table/tr[position()>1]" startNode:nil];
		for(NSValue* rowValue in torrentRows)
		{
			//
			// parse torrent info
			//
			@autoreleasepool
			{
				xmlNodePtr xmlRow											= (xmlNodePtr)[rowValue pointerValue];
				UserTorrentInfo* info										= [[[UserTorrentInfo alloc] init] autorelease];
				info.category												= [self.xmlParser stringFromPath:@"./td[1]/a" startNode:xmlRow];
				NSString* temp												= [self.xmlParser stringFromPath:@"./td[2]/a/@href" startNode:xmlRow];
				info.torrentId												= [[temp substringWithRange:NSMakeRange(15, temp.length - 15 - 6)] integerValue];
				info.title													= [self.xmlParser stringFromPath:@"./td[2]/a/@title" startNode:xmlRow];
				info.seederCount											= -1;
				info.leecherCount											= -1;
				info.magicExpire											= [self.xmlParser stringFromPath:@"./td[2]/b[last()]/span/@title" startNode:xmlRow];
				info.upRate													= [self.xmlParser stringFromPath:@"./td[2]/img[@class='arrowup']/following-sibling::b[1]" startNode:xmlRow];
				info.downRate												= [self.xmlParser stringFromPath:@"./td[2]/img[@class='arrowdown']/following-sibling::b[1]" startNode:xmlRow];
				if(!info.upRate && !info.downRate)
				{
					info.upRate												= [NexusClient parseFreeTypeForImgClass:[self.xmlParser stringFromPath:@"./td[2]/img[@class!='arrowup' and @class!='arrowdown']/@class" startNode:xmlRow]];
					if(!info.upRate && info.magicExpire)
						info.upRate											= [NexusClient parseFreeTypeForFontClass:[self.xmlParser stringFromPath:@"./td[2]/b[last()-1]/font/@class" startNode:xmlRow]];
					if(!info.upRate)
						info.upRate											= [NexusClient parseFreeTypeForFontClass:[self.xmlParser stringFromPath:@"./td[2]/b[last()]/font/@class" startNode:xmlRow]];
					if(!info.upRate)
						info.upRate											= [NexusClient parseFreeTypeForRowClass:[self.xmlParser stringFromPath:@"./@class" startNode:xmlRow]];
				}
				
				if([self.type isEqualToString:@"uploaded"])
				{
					info.size												= [self.xmlParser stringFromPath:@"./td[3]" startNode:xmlRow];
					info.seederCount										= [[self.xmlParser stringFromPath:@"./td[4]" startNode:xmlRow] integerValue];
					info.leecherCount										= [[self.xmlParser stringFromPath:@"./td[5]" startNode:xmlRow] integerValue];
					info.uploadedSize										= [self.xmlParser stringFromPath:@"./td[7]" startNode:xmlRow];
					info.seedTime											= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[8]" startNode:xmlRow] decodeTime]];
				}
				else if([self.type isEqualToString:@"seeding"] || [self.type isEqualToString:@"leeching"])
				{
					info.size												= [self.xmlParser stringFromPath:@"./td[3]" startNode:xmlRow];
					info.seederCount										= [[self.xmlParser stringFromPath:@"./td[4]" startNode:xmlRow] integerValue];
					info.leecherCount										= [[self.xmlParser stringFromPath:@"./td[5]" startNode:xmlRow] integerValue];
					info.uploadedSize										= [self.xmlParser stringFromPath:@"./td[7]" startNode:xmlRow];
					info.downloadedSize										= [self.xmlParser stringFromPath:@"./td[8]" startNode:xmlRow];
					info.shareRatio											= NSLocalizedString([self.xmlParser stringFromPath:@"./td[9]" startNode:xmlRow], nil);
				}
				else if([self.type isEqualToString:@"completed"])
				{
					info.uploadedSize										= [self.xmlParser stringFromPath:@"./td[4]" startNode:xmlRow];
					info.seedTime											= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[5]" startNode:xmlRow] decodeTime]];
					info.leechTime											= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[6]" startNode:xmlRow] decodeTime]];
					info.completeTime										= [self.xmlParser stringFromPath:@"./td[7]/span/@title" startNode:xmlRow];
					if(!info.completeTime.length)
						info.completeTime									= [self.xmlParser stringFromPath:@"./td[6]" startNode:xmlRow];
				}
				else if([self.type isEqualToString:@"incomplete"])
				{
					info.uploadedSize										= [self.xmlParser stringFromPath:@"./td[4]" startNode:xmlRow];
					info.downloadedSize										= [self.xmlParser stringFromPath:@"./td[5]" startNode:xmlRow];
					info.shareRatio											= NSLocalizedString([self.xmlParser stringFromPath:@"./td[6]" startNode:xmlRow], nil);
					info.leechTime											= [NSString formatTime:[[self.xmlParser stringFromPath:@"./td[7]" startNode:xmlRow] decodeTime]];
				}
				
				//
				// add to list
				//
				[self.torrentList addObject:info];
			}
		}
	}
	
	[super connectionDidFinishLoading:connection];
}

@end
